package leetCode.offer48;

import java.util.HashMap;
import java.util.Map;

public class Solution1 implements Solution{

    @Override
    public int lengthOfLongestSubstring(String s) {
        int max = 0;
        int result = 0;
        Map<Character, Integer> subMap = new HashMap<>();
        int limit = 0;
        for(int i=0;i<s.length();i++){
            if(!subMap.containsKey(s.charAt(i))){
                result++;
                if(result>max)
                    max = result;
            }else{
                int index = subMap.get(s.charAt(i));
                result = i-index;
                for(int j = limit ; j<=index;j++){
                    subMap.remove(s.charAt(j));
                }
                limit = index+1;
            }
            subMap.put(s.charAt(i),i);
        }
        return max;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1().lengthOfLongestSubstring( "dvdf"));
    }

}
